<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


class NewYearGoods extends Base
{
    const NEW_YEAR_GOODS_ROOM_SCORE = 'NEW:YEAR:GOODS:ROOM:SCORE';//延时队列开奖房间集合
    const NEW_YEAR_GOODS_ROOM_LIST = 'NEW:YEAR:GOODS:ROOM:INFO';//延时队列开奖房间单独信息
    const NEW_YEAR_GOODS_POND = 'NEW:YEAR:GOODS:POND';//奖池
    const NEW_YEAR_GOODS_POND_CONSUMER = 'NEW:YEAR:GOODS:POND:CONSUMER';//输出奖池
    const NEW_YEAR_GOODS_USER_PARTAKE = 'NEW:YEAR:GOODS:PARTAKE';//用户参与房间活动
    const NEW_YEAR_GOODS_USER_PARTAKE_LOG = 'NEW:YEAR:GOODS:PARTAKE:LOG';//用户参与房间活动记录
    const NEW_YEAR_GOODS_USER_AWARD_LOG = 'NEW:YEAR:GOODS:USER:AWARD:LOG';//用户获奖记录
    const NEW_YEAR_GOODS_ROOM_PROFIT_LOG = 'NEW:YEAR:GOODS:ROOM:PROFIT:LOG';//房间盈亏记录

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    //生成房间内礼包信息存入有序集合
    public function createRoomBagZADD($room_id, $score)
    {
        return Redis::ZADD(self::NEW_YEAR_GOODS_ROOM_SCORE, $score, $room_id);
    }


    //删除房间有序记录
    public function delRoomScore($room_id)
    {
        return Redis::ZREM(self::NEW_YEAR_GOODS_ROOM_SCORE, $room_id);
    }

    //获取房间活动到期时间
    public function getRoomScore($room_id)
    {
        $key = self::NEW_YEAR_GOODS_ROOM_SCORE;
        return Redis::ZSCORE($key, $room_id);
    }

    //获取到期礼包房间
    public function getEndDateRoomIds($time = '')
    {
        if (empty($time)) {
            $time = time();
        }
        $key = self::NEW_YEAR_GOODS_ROOM_SCORE;
        return Redis::ZRANGEBYSCORE($key, 0, $time, 'WITHSCORES');
    }

    /*
     * 插入参与年货房间合集
     * @param $room_id 房间id
     * @param $type   开启类型
     * @param $diamond //支付钻石
     * @param $num  //奖池剩余礼物数量
     * @param $endTimes  //结束时间
     * @param $user_id  //开启用户id
     * */
    public function insertBagToRoom($room_id, $type, $diamond, $num, $endTimes, $user_id)
    {
        $key = self::NEW_YEAR_GOODS_ROOM_LIST . ':' . $room_id;
        $time = time() + $endTimes;
        Redis::HSET($key, 'type', $type);
        Redis::HSET($key, 'diamond', $diamond);//支出
        Redis::HSET($key, 'surplus', $num);//库存
        Redis::HSET($key, 'profit', 0); // 利润
        Redis::HSET($key, 'user_id', $user_id); // 开启用户
        Redis::HSET($key, 'endTimes', $time); // 开启用户
        return $this->ttlRedisKey($key, $endTimes + 180);
    }


    //获取房间开启类型
    public function getBagRoomType($room_id)
    {
        $key = self::NEW_YEAR_GOODS_ROOM_LIST . ':' . $room_id;
        return Redis::HGETALL($key);
    }

    //
    public function awardLog($room_id)
    {
        $redisKeyLock = 'lock:' . 'CACHE:AWARD:GIFT' . $room_id;
        $this->ttlRedisKey($redisKeyLock, 5);

        return Redis::GET($redisKeyLock);

    }

    //礼包开启房间库存
    public function decBagRoomInfo($room_id, $k = 'surplus', $num = 1)
    {
        $key = self::NEW_YEAR_GOODS_ROOM_LIST . ':' . $room_id;
        $cache = Redis::HGETALL($key);
        if (count($cache) != 6) {
            return false;
        }
        $exists = Redis::HEXISTS($key, $k);
        if ($exists) {
            return Redis::HINCRBY($key, $k, intval($num));
        }
        return true;
    }

    //删除礼包开启房间
    public function delBagRoomType($room_id)
    {
        $key = self::NEW_YEAR_GOODS_ROOM_LIST . ':' . $room_id;
        return Redis::DEL($key);
    }

    /*
     * 生成奖池
     * @param $gift_id 大礼物id
     * @param $surplus_id 补充礼物id
     * @param $surplus_num 补充礼物数量
     * @param $radioNum 比例开奖数量
     *
     * */
    public function createPond($room_id, $gift_id, $surplus_ids, $surplus_num, $radioNum)
    {
        $key = self::NEW_YEAR_GOODS_POND . ':' . $room_id;
        $key2 = self::NEW_YEAR_GOODS_POND_CONSUMER . ':' . $room_id;
        if ($gift_id == 0) {
            //无大礼物
            array_shift($surplus_num);
            //个数加1
            $count = count($surplus_num) - 1;
            $surplus_num[$count] += 1;
        } else {
            array_unshift($surplus_ids, $gift_id);
        }
        $pond_arr = [];
        foreach ($surplus_num as $k => $v) {
            $arr = array_fill(0, $v, $surplus_ids[$k]);
            $pond_arr = array_merge($pond_arr, $arr);
        }
        shuffle($pond_arr);
        $pond_arr = array_slice($pond_arr, 0, $radioNum);
        Redis::LPUSH($key, $pond_arr);
        Redis::LPUSH($key2, $pond_arr);
        $this->ttlRedisKey($key2);
        return $this->ttlRedisKey($key);
    }

    //出奖池
    public function popPond($room_id)
    {
        $key = self::NEW_YEAR_GOODS_POND . ':' . $room_id;
        return Redis::LPOP($key);
    }

    //出奖池
    public function popConsumerPond($room_id)
    {
        $key = self::NEW_YEAR_GOODS_POND_CONSUMER . ':' . $room_id;
        return Redis::LPOP($key);
    }

    public function delConsumerPond($room_id)
    {
        $key = self::NEW_YEAR_GOODS_POND_CONSUMER . ':' . $room_id;
        return Redis::DEL($key);
    }

    //入奖池
    public function pushConsumerPond($room_id, $gift_id)
    {
        $key = self::NEW_YEAR_GOODS_POND_CONSUMER . ':' . $room_id;
        return Redis::LPUSH($key, $gift_id);
    }

    //入奖池
    public function pushPond($room_id, $gift_id)
    {
        $key = self::NEW_YEAR_GOODS_POND . ':' . $room_id;
        return Redis::LPUSHX($key, $gift_id);
    }

    //查案房间奖励池子剩余数量
    public function getPondLen($room_id)
    {
        $key = self::NEW_YEAR_GOODS_POND . ':' . $room_id;
        return Redis::LLEN($key);
    }

    //删除房间奖励池子
    public function delPond($room_id)
    {
        $key = self::NEW_YEAR_GOODS_POND . ':' . $room_id;
        return Redis::DEL($key);
    }

    //用户参与房间
    public function pushPartakeToRoom($room_id, $user_id)
    {
        $key = self::NEW_YEAR_GOODS_USER_PARTAKE . ':' . $room_id;
        return Redis::LPUSH($key, $user_id);
    }

    //用户开奖 pop用户
    public function popPartakeToRoom($room_id)
    {
        $key = self::NEW_YEAR_GOODS_USER_PARTAKE . ':' . $room_id;
        return Redis::LPOP($key);
    }

    //获取用户参与记录
    public function getRoomPartakeUser($room_id)
    {
        $key = self::NEW_YEAR_GOODS_USER_PARTAKE . ':' . $room_id;
        return Redis::LRANGE($key, 0, -1);
    }

    //删除用户参与记录
    public function delPartakeLog($room_id)
    {
        $key = self::NEW_YEAR_GOODS_USER_PARTAKE . ':' . $room_id;
        return Redis::DEL($key);
    }

    //用户获奖记录
    public function createAwardLog($user_id, $gift_id, $type)
    {
        $key = self::NEW_YEAR_GOODS_USER_AWARD_LOG . '-' . $user_id;
        $k = $gift_id . '-' . time() . '-' . rand(1, 1000000);
        Redis::HSET($key, $k, $type);
        return $this->ttlRedisKey($key);
    }

    //用户获奖记录
    public function getUserAwardLog($user_id)
    {
        $key = self::NEW_YEAR_GOODS_USER_AWARD_LOG . '-' . $user_id;
        return Redis::HGETALL($key);
    }

    //用户房间盈亏记录
    public function createRoomProfitLog($room_id, $profit, $type)
    {
        $key = self::NEW_YEAR_GOODS_ROOM_PROFIT_LOG . '-' . $room_id;
        $k = $type . '-' . time() . '-' . rand(1, 10000);
        Redis::HSET($key, $k, $profit);
        return $this->ttlRedisKey($key);
    }

    //房间开启记录
    public function getRoomOpenLog($room_id)
    {
        $key = self::NEW_YEAR_GOODS_ROOM_PROFIT_LOG . '-' . $room_id;
        return Redis::HGETALL($key);
    }

    //用户参与记录
    public function createUserPartakeLog($user_id, $room_id, $type)
    {
        $key = self::NEW_YEAR_GOODS_USER_PARTAKE_LOG . '-' . $user_id;
        $this->ttlRedisKey($key);
        $k = $room_id . '-' . $type . '-0'; //房间-类型-状态
        Redis::LPUSH($key, $k);
        return $this->ttlRedisKey($key);
    }

    //获取用户参与房间礼包的记录
    public function getUserPartakeLog($user_id)
    {
        $key = self::NEW_YEAR_GOODS_USER_PARTAKE_LOG . '-' . $user_id;
        return Redis::LRANGE($key, 0, -1);
    }

    //更新记录状态  删除原有的 添加新的
    public function updateUserPartakeLog($user_id, $k1, $k2)
    {
        $key = self::NEW_YEAR_GOODS_USER_PARTAKE_LOG . '-' . $user_id;
        Redis::LREM($key, 1, $k1);//删除第一个未开奖记录
        $this->ttlRedisKey($key);
        return Redis::RPUSH($key, $k2);//将已开启状态插入记录最后
    }

    public function pondLog($type, $goods, $tw, $diamond, $ratio)
    {
        $key = 'testpond' . $goods . '-' . $tw . '-' . $ratio;
        return Redis::HINCRBY($key, $type, $diamond);
    }

    public function getPondLog($goods, $tw, $ratio)
    {
        $key = 'testpond' . $goods . '-' . $tw . '-' . $ratio;
        return Redis::HGETALL($key);
    }
}